/*
 * Copyright 2012 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.chacha.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.chacha.model.Account;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import de.undercouch.bson4jackson.BsonFactory;
import de.undercouch.bson4jackson.BsonGenerator;

/**
 *  seq     Bson4Jackson(ms)    json(ms)
 *---------------------------------------------------
 *   1  	21328				10860
 *   2		21688				10812
 *   3		21813				11031
 *---------------------------------------------------
 *   avg	21610				10901
 * 
 * bson复用stream
 *  seq     Bson4Jackson()ms    json(ms)
 *---------------------------------------------------
 *   1  	21343				10750
 *   2		21296				10891
 *   3		21297				10844
 *---------------------------------------------------   
 * avg		21312				10828
 *	@author: hejing
 *	@date: 2012-11-20-上午08:48:51
 */
public class PerformanceBsonJson {
	private Account src=new Account();
	private Account dst=new Account();
	
	@Before
	public void setUp(){
		src.setUserName("hejing");
		src.setPasswd("ddddddd");
	}
	
	@Test
	public void testJson() throws JsonMappingException, JsonGenerationException, IOException{
		long begin=System.currentTimeMillis();
		for(int i=0;i<1000000;i++){
			String jsonString=JsonPojoMapper.toJson(src,true);
			dst=(Account)JsonPojoMapper.fromJson(jsonString, Account.class);
		}
		long end=System.currentTimeMillis();
		System.out.println("json="+(end-begin));
	}
	
	
	@Test
	public void testBson4Jackson() throws JsonGenerationException, JsonMappingException, IOException{
		long begin=System.currentTimeMillis();
	    BsonFactory fac=new BsonFactory();
	    fac.enable(BsonGenerator.Feature.ENABLE_STREAMING);
	    ObjectMapper mapper = new ObjectMapper(fac);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
		for(int i=0;i<1000000;i++){
		    mapper.writeValue(baos, src);
		    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		    dst = mapper.readValue(bais, Account.class);
		    baos.reset();
		}
		long end=System.currentTimeMillis();
		System.out.println("bson4Jackson="+(end-begin));
	}
}
